package day13.demo1.maipiao.demo4;

/**
 * @Auther:小陈的Java
 */
public class WindowTest4 {
    public static void main(String[] args) {
        Runnable runnable = new Window4();
        new Thread(runnable,"窗口1").start();
        new Thread(runnable,"窗口2").start();
        new Thread(runnable,"窗口3").start();
    }
}
class Window4 implements Runnable{
    private int ticket =100;
    private boolean tag = true;

    @Override
    public void run() {
        while (tag){
            sell();
        }
    }

    private synchronized void sell() {
        if(ticket>0){
            try {
                System.out.println(Thread.currentThread().getName()+":卖票，卖第"+ticket+"票");
                Thread.sleep(10);
                ticket--;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else {
            tag = false;
        }
    }
}